//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-09-04  Jed Young  Creation
//

using chunmapModel
using chunmapData
using gfx2
using gfx
using gfx2Imp
using web
using chunmapView
using chunmapRaster

**
** Capabilities
**
const class Capabilities : Weblet
{

  private static const Str version := "1.1.1"

  const Str serviceTile
  const Str srs
  const Unsafe layers

  Uri? link() { req.headers["Host"]?.toUri }
  Uri url() { ("http://" + req.headers["Host"] + req.absUri.pathOnly).toUri }

  LayerList layerList() { layers.val }

  new make(LayerList layers, Str serviceTile, Int srs := 4326)
  {
    this.layers = Unsafe(layers)
    this.serviceTile = serviceTile
    this.srs = srs.toStr
  }

  override Void onService()
  {
    res.headers["Content-Type"] = MimeType.forExt("xml").toStr
    head(res.out)
    layerList.each |layer|
    {
      body(res.out, layer)
    }
    foot(res.out)
  }

  private Void head(OutStream out)
  {
    out.printLine("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>")
    out.printLine("<WMT_MS_Capabilities version=\"" + version + "\">")
    out.printLine("<Service>")
    out.printLine("  <Name>OGC:WMS</Name>")
    out.printLine("  <Title>" + serviceTile + "</Title>")
    out.printLine("  <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
        + link + "\" />")
    out.printLine("  <Fees>none</Fees>")
    out.printLine("  <AccessConstraints>none</AccessConstraints>")
    out.printLine("</Service>")
    out.printLine("<Capability>")
    out.printLine("  <Request>")
    out.printLine("  <GetCapabilities>")
    out.printLine("    <Format>application/vnd.ogc.wms_xml</Format>")
    out.printLine("    <DCPType>")
    out.printLine("    <HTTP>")
    out.printLine("      <Get>")
    out.printLine("      <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
        + url + "\"/>")
    out.printLine("      </Get>")
    out.printLine("    </HTTP>")
    out.printLine("    </DCPType>")
    out.printLine("  </GetCapabilities>")
    out.printLine("  <GetMap>")
    out.printLine("    <Format>image/jpg</Format>")
    out.printLine("    <Format>image/png</Format>")
    out.printLine("    <DCPType>")
    out.printLine("    <HTTP>")
    out.printLine("      <Get>")
    out.printLine("      <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
        + url + "\"/>")
    out.printLine("      </Get>")
    out.printLine("    </HTTP>")
    out.printLine("    </DCPType>")
    out.printLine("  </GetMap>")
    out.printLine("  <GetFeatureInfo>")
    out.printLine("    <Format>text/plain</Format>")
    out.printLine("    <Format>text/html</Format>")
    out.printLine("    <DCPType>")
    out.printLine("    <HTTP>")
    out.printLine("      <Get>")
    out.printLine("      <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
        + url + "\"/>")
    out.printLine("      </Get>")
    out.printLine("    </HTTP>")
    out.printLine("    </DCPType>")
    out.printLine("  </GetFeatureInfo>")
    out.printLine("  </Request>")
    out.printLine("  <Exception>")
    out.printLine("  <Format>application/vnd.ogc.se_xml</Format>")
    out.printLine("  </Exception>")
    out.printLine(" <Layer>")
    out.printLine("  <Title>" + serviceTile + "</Title>")
    out.printLine("  <SRS>" + srs + "</SRS>")
  }

  private Void body(OutStream out, Layer layer)
  {
    out.printLine("  <Layer>")
    out.printLine("  <Name>" + layer.name + "</Name>")
    out.printLine("    <Title>" + layer.name + "</Title>")
    Envelope env := layer.envelope
    out.printLine("    <LatLongBoundingBox minx=\"" + env.minX
      + "\" miny=\"" + env.minY + "\" maxx=\"" + env.maxX
      + "\" maxy=\"" + env.maxY + "\" />")

    // do not support style
    // out.printLine("    <Style>")
    // out.printLine("    <Name>" + "?" + "</Name>")
    // out.printLine("    <Title>" + "?" + "</Title>")
    // out.printLine("    </Style>")

    out.printLine("  </Layer>")
  }

  private Void foot(OutStream out)
  {
    out.printLine(" </Layer>")
    out.printLine("</Capability>")
    out.printLine("</WMT_MS_Capabilities>")
  }
}